﻿using HandyControl.Controls;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AgileLogistics.View.Meishi
{
    /// <summary>
    /// PallPage.xaml 的交互逻辑
    /// </summary>
    public partial class PallPage : Page
    {
        public PallPage()
        {
            InitializeComponent();
            vm = this.DataContext as ViewModel.PollViewModel;
            //获取数据
        }


        ViewModel.PollViewModel vm;

        Model.DBContext dbContext = new Model.DBContext();
        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            vm.CurrentPoll = dbContext.DB.Queryable<Model.Poll>().First(p => p.IsPublic==true);

            vm.CurrentPoll.FangwenCount += 1;
            dbContext.DB.Updateable<Model.Poll>(vm.CurrentPoll).ExecuteCommand();
            vm.TouPiaoCount = dbContext.DB.Queryable<Model.PollItem>().Where(it=>it.PollId==vm.CurrentPoll.Id).Sum(it => it.PollNum);
            vm.FangwenCount = vm.CurrentPoll.FangwenCount;
            vm.CanyuCount = dbContext.DB.Queryable<Model.Users>().Count();


            vm.ZhucaiMenu =new  ObservableCollection<Model.PollItem>( dbContext.DB.Queryable<Model.PollItem>().Where(it => it.PollId == vm.CurrentPoll.Id && it.ClassId == 2).ToList());
            vm.ZhushiMenu =new  ObservableCollection<Model.PollItem>( dbContext.DB.Queryable<Model.PollItem>().Where(it => it.PollId == vm.CurrentPoll.Id && it.ClassId == 3).ToList());
            vm.TangMenu =new  ObservableCollection<Model.PollItem>( dbContext.DB.Queryable<Model.PollItem>().Where(it => it.PollId == vm.CurrentPoll.Id && it.ClassId == 4).ToList());
 
        }

        private async void btn_TouPiao(object sender, RoutedEventArgs e)
        {
            try
            {
                //判断是否达到指定票数
                var userpoll = dbContext.DB.Queryable<Model.UserPoll>().Where(p => p.UserId == Comm.Temp.LoginUser.Id).First();
                if (userpoll == null)
                {
                    userpoll = new Model.UserPoll
                    {
                        UserId = Comm.Temp.LoginUser.Id,
                        ZCPollCount = 0,
                        ZSPollCount = 0,
                        TangPollCount = 0
                    };
                }



                // Growl.SuccessGlobal("投票");
                var btn = sender as Button;
                var item = btn.Tag as Model.PollItem;
                if (item.ClassId == 2)
                {
                    if (userpoll.ZCPollCount >= 4)
                    {
                        Growl.InfoGlobal("您的投票数量已超过4票，不能重复投票。");
                        return;
                    }
                    else
                    {
                        userpoll.ZCPollCount += 1;

                    }
                }




                if (item.ClassId == 3)
                {
                    if (userpoll.ZSPollCount >= 3)
                    {
                        Growl.InfoGlobal("您的投票数量已超过3票，不能重复投票。");
                        return;
                    }
                    else
                    {
                        userpoll.ZSPollCount += 1;

                    }
                }
                if (item.ClassId == 4)
                {
                    if (userpoll.TangPollCount >= 2)
                    {
                        Growl.InfoGlobal("您的投票数量已超过2票，不能重复投票。");
                        return;
                    }
                    else
                    {
                        userpoll.TangPollCount += 1;

                    }
                }



                item.PollNum += 1;
                vm.TouPiaoCount += 1;

                await dbContext.DB.Updateable<Model.PollItem>(item).ExecuteCommandAsync();
                await dbContext.DB.Saveable<Model.UserPoll>(userpoll).ExecuteCommandAsync();
            }
            catch (Exception ex)
            {

                Growl.ErrorGlobal(ex.Message);
            }

          
           
        }
    }
}
